<?php
namespace app\index\service;
use think\Request;
use think\Loader;
use think\Url;
use Wx\Wechat;

Loader::import('WxPay.WxPay', EXTEND_PATH, '.Api.php');
/**
 * xushuhui
 * 2017/8/9
 * 16:03
 */
class WechatPay
{
	//生成微信预订单
	public static function makeWxPreOrder($order=[],$openid='',$notifyUrl='')
	{
		$money = $order['money']*100;
		$input = new \WxPayUnifiedOrder();
		$input->SetBody("和诚云");
		$input->SetOut_trade_no($order['orderNo']);
		$input->SetTotal_fee("$money");
		$input->SetNotify_url("$notifyUrl");
		$input->SetTrade_type("JSAPI");
		$input->SetProduct_id($order['id']);
		$input->SetOpenid($openid);
		$orderData = \WxPayApi::unifiedOrder($input);
		
		$tools = new \JsApiPay();
		$jsApiParameters = $tools->GetJsApiParameters($orderData);
		return $jsApiParameters;
	}
	//发送微信模板消息
	public static function sendWxMessage($openid='',$template_id='',$JumpUrl='',$data=array())
	{
		$app_id = config('wx.app_id');
		$app_secret = config('wx.app_secret');
		$res = Wechat::instance($app_id,$app_secret)->sendTemplateMsg($openid,$template_id,$JumpUrl,$data);
		return $res;
	}
	//微信红包
	public static function makeWxRedPack($money=0,$orderNo,$openid='')
	{
		$money = $money*100;
		$input = new \WxPayRedPack();
		$input->SetMchBillNo($orderNo);
		$input->SetTotalAmount("$money");
		$input->SetTotalNum(1);//人数
		$input->SetReopenid($openid);
		$input->SetWishing("恭喜发财");
		$input->SetSendName("卡友管家");
		$input->SetActName("卡友管家抽奖");
		$input->SetRemark("卡友管家恭喜发财");
		$input->SetClientip("0.0.0.0");
		$orderData = \WxPayApi::sendRedPack($input);
		return $orderData;
	}
}